package util

import (
	"crypto/md5"
	"fmt"
	"net/url"
	"sort"
)

type Signer struct {
	secret string
}

func NewSigner(secret string) *Signer { return &Signer{secret: secret} }

func (s *Signer) Sign(data url.Values) string {
	keys := []string{}
	for k := range data {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	str := ""
	for _, k := range keys {
		str += k + "=" + data.Get(k)
	}
	return fmt.Sprintf("%x", md5.Sum([]byte(str+s.secret)))
}
